package com.wjp.villagemp.controller.manage;

import com.wjp.villagemp.model.sys.PageResult;
import com.wjp.villagemp.service.manage.ResultAnalyticService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.web.PageableDefault;
import org.springframework.web.bind.annotation.*;

import java.util.Iterator;
import java.util.List;
import java.util.Map;

/**
 * @Author: lixr
 * @Date: 2019/1/14 12:21
 * @Description:
 */
@RestController
@RequestMapping("/result")
public class ResultAnalyticController {
    @Autowired
    private ResultAnalyticService resultAnalyticService;

    @GetMapping
    public PageResult resultList(@PageableDefault(page = 0, size = 10, sort = "class_rank", direction = Sort.Direction.ASC) Pageable pageable) {
        PageResult pageResult = new PageResult();
        int pageSize = pageable.getPageSize();
        String sort = pageable.getSort().toString();
        int index = sort.indexOf(":");
        String sortField = sort.substring(0, index);
        String direction = sort.substring(index+2);
        List<Map<String, Object>> resultList = resultAnalyticService.resultList(pageSize, pageable.getPageNumber() * pageSize, sortField, direction);
        int resultSize = resultAnalyticService.resultList(0, 0, sortField, direction).size();
        pageResult.setData(resultList);
        pageResult.setTotalCount(resultSize);
        return pageResult;
    }
}
